VFG.PModel=function(viewer, options) {
	this.viewer = viewer;
	this.options = options;
	this.parentId= options.parentId||'';
	this.id= options.id;
	this.name= options.name||'';
	this.code= options.code||'';
	this.type= options.type||'';
	this.position=options.position||null;
	this.orientation=options.orientation||{};
	this.model=options.thing||{};
	this.attribute=options.attribute||{};
	this.propertys=options.propertys||[];
	this.init();
}
VFG.PModel.prototype.init = function() {
	let _this=this;
	var param={
			id: this.id,
			name :this.name,
			url :this.model.url||'',
			modelMatrix : this.getModelMatrix(),
			scale :this.model.scale*1||1,
			show : this.model.show||true,   
			minimumPixelSize:this.model.minimumPixelSize||0,
			allowPicking : this.model.allowPicking||true,        
			debugShowBoundingVolume : this.model.debugShowBoundingVolume||false, 
			debugWireframe : this.model.debugWireframe||false, 
			backFaceCulling:this.model.backFaceCulling||false, 
			shadows: this.model.shadows||false,   
	}
	if('1'== this.model.distanceDisplayCondition){
		param.distanceDisplayCondition=VFG.Util.getDistanceDisplayCondition( this.model.distanceNear, this.model.distanceFar);	
	}
	if('1'== this.model.silhouette){
		param.silhouetteColor= this.model.silhouetteColor? Cesium.Color.fromCssColorString( this.model.silhouetteColor):Cesium.Color.BLACK;	
		param.silhouetteSize= this.model.silhouetteSize*1||0;	
	}
	if('1'== this.model.fill){
		param.color= this.model.fillColor? Cesium.Color.fromCssColorString( this.model.fillColor):Cesium.Color.BLACK;	
	}
	this.primity = this.viewer.scene.primitives.add(Cesium.Model.fromGltf(param));
	this.primity.readyPromise.then(function(model) {
		if(_this.success){
			_this.success(model)
		}
	}).otherwise(function(error){
		if(_this.error){
			_this.error(error);
		}
    });
};
VFG.PModel.prototype.getModelMatrix=function(){
    var hpRoll = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(this.orientation.heading || 0), Cesium.Math.toRadians(this.orientation.pitch || 0), Cesium.Math.toRadians(this.orientation.roll || 0));
    var fixedFrameTransform = Cesium.Transforms.eastNorthUpToFixedFrame;
    let cartesian3=Cesium.Cartesian3.fromDegrees(this.position.x*1, this.position.y*1,this.position.z*1);
    var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(cartesian3, hpRoll, this.viewer.scene.globe.ellipsoid, fixedFrameTransform);
    return modelMatrix;
}

VFG.PModel.prototype.getRadius=function(){
    return this.primity.boundingSphere.radius;
}


VFG.PModel.prototype.on=function(type,callback){
	if('success'==type){
		this.success=callback;
	}
	if('error'==type){
		this.error=callback;
	}
}

VFG.PModel.prototype.update=function(param){

	if(param.position){
		this.position=param.position;
	}
	
	if(param.orientation){
		if(this.orientation.pitch){
			this.orientation.pitch=param.orientation.pitch;
		}
		if(this.orientation.heading){
			this.orientation.heading=param.orientation.heading;
		}
		if(this.orientation.roll){
			this.orientation.roll=param.orientation.roll;
		}
	}
	
	if(this.primity){
		this.primity.modelMatrix=this.getModelMatrix();
	}
}

VFG.PModel.prototype.updatePosition=function(position){
	this.position=position;
	if(this.primity){
		this.primity.modelMatrix=this.getModelMatrix();
	}
}

VFG.PModel.prototype.updateAttribute=function(attribute){
	this.attribute=attribute;
	if(attribute && attribute.name){
		this.name=attribute.name;
		this.attribute.name=attribute.name;
	}
	if(attribute && attribute.parentId){
		this.parentId=attribute.parentId;
		this.attribute.parentId=attribute.parentId;
	}
	
	if(attribute && attribute.code){
		this.attribute.code=attribute.code;
		this.code=attribute.code;
	}
	if(attribute && attribute.sort){
		this.attribute.sort=attribute.sort;
	}
}

/**
 * 缩放
 */
VFG.PModel.prototype.updateScale=function(scale){
	this.model.scale=scale;
	if(this.primity){
		this.primity.scale=this.model.scale*1;
	}
}

VFG.PModel.prototype.updateHeading=function(heading){
	this.orientation.heading=heading;
	if(this.primity){
		this.primity.modelMatrix=this.getModelMatrix();
	}
}

VFG.PModel.prototype.updatePitch=function(pitch){
	this.orientation.pitch=pitch;
	if(this.primity){
		this.primity.modelMatrix=this.getModelMatrix();
	}
}

VFG.PModel.prototype.updateRoll=function(roll){
	this.orientation.roll=roll;
	if(this.primity){
		this.primity.modelMatrix=this.getModelMatrix();
	}
}

VFG.PModel.prototype.updateMinimumPixelSize=function(minimumPixelSize){
	this.model.minimumPixelSize=minimumPixelSize;
	if(this.primity){
		this.primity.minimumPixelSize=minimumPixelSize;
	}
}

VFG.PModel.prototype.updateColor=function(color){
	this.model.fillColor=color;
	if(this.primity){
		this.primity.color=color? Cesium.Color.fromCssColorString(color):Cesium.Color.WHITE;	
	}
}

VFG.PModel.prototype.updateSilhouetteColor=function(silhouetteColor){
	this.model.silhouetteColor=silhouetteColor;
	if(this.primity){
		this.primity.silhouetteColor=silhouetteColor? Cesium.Color.fromCssColorString(silhouetteColor):Cesium.Color.WHITE;	
	}
}
VFG.PModel.prototype.updateSilhouetteSize=function(silhouetteSize){
	this.model.silhouetteSize=silhouetteSize;
	if(this.primity){
		this.primity.silhouetteSize=silhouetteSize*1;	
	}
}

VFG.PModel.prototype.updateDistanceDisplay=function(display){
	if(display.distanceNear){
		this.model.distanceNear=display.distanceNear;
	}
	if(display.distanceFar){
		this.model.distanceFar=display.distanceFar;
	}
	if(this.primity){
		this.primity.distanceDisplayCondition=VFG.Util.getDistanceDisplayCondition(this.model.distanceNear||0,this.model.distanceFar||5000);	
	}
}

VFG.PModel.prototype.isDistanceDisplay=function(display){
	this.model.distanceDisplayCondition=display;
	if(this.primity){
		if('1'===display){
			this.primity.distanceDisplayCondition=VFG.Util.getDistanceDisplayCondition(this.model.distanceNear||0,this.model.distanceFar||5000);	
		}else{
			this.primity.distanceDisplayCondition=null;	
		}
	}
}

VFG.PModel.prototype.isSilhouette=function(silhouette){
	this.model.silhouette=silhouette;
	if(this.primity){
		if('1'===silhouette){
			this.primity.silhouetteColor= this.model.silhouetteColor? Cesium.Color.fromCssColorString( this.model.silhouetteColor):Cesium.Color.WHITE;	
			this.primity.silhouetteSize= this.model.silhouetteSize*1||0;	
		}else{
			this.primity.silhouetteColor= Cesium.Color.WHITE;	
			this.primity.silhouetteSize= 0;	
		}
	}
}

VFG.PModel.prototype.isFill=function(fill){
	this.model.fill=fill;
	if(this.primity){
		if('1'===fill){
			this.primity.color= this.model.fillColor? Cesium.Color.fromCssColorString( this.model.fillColor):Cesium.Color.WHITE;	
		}else{
			this.primity.color= Cesium.Color.WHITE;	
		}
	}
}

VFG.PModel.prototype.isShadows=function(shadows){
	this.model.shadows=shadows;
	if(this.primity){
		if('1'===shadows){
			this.primity.shadows= this.model.shadows*1;	
		}else{
			this.primity.shadows= this.model.shadows*1;	
		}
	}
}

VFG.PModel.prototype.selected=function(){
	if(this.primity){
		this.primity.silhouetteColor= Cesium.Color.YELLOW;	
		this.primity.silhouetteSize= 2;	
	}
}

VFG.PModel.prototype.unselected=function(){
	if(this.primity){
		this.primity.silhouetteColor= this.model.silhouetteColor? Cesium.Color.fromCssColorString( this.model.silhouetteColor):Cesium.Color.WHITE;	
		this.primity.silhouetteSize= 0;	
	}
}

VFG.PModel.prototype.updateProperty= function(property) {
	var _this = this;
	if(_this.propertys){
		for(let index in _this.propertys){
			if(property.id==_this.propertys[index].id){
				_this.propertys.splice(index,1,property);
				return;
			}
		}
		_this.propertys.push(property);
	}
};

VFG.PModel.prototype.removeProperty= function(id) {
	var _this = this;
	if(_this.propertys){
		for(let index in _this.propertys){
			if(id==_this.propertys[index].id){
				_this.propertys.splice(index,1);
				return;
			}
		}
	}
};

VFG.PModel.prototype.destroy = function() {
	let _this=this;
	if(this.primity){
		this.viewer.scene.primitives.remove(this.primity);
	}
	if(this.antennaMap){
		_this.antennaMap.forEach(function (value,key){
			try{
				_this.viewer.entities.removeById(key);
			}catch (e) {
			}finally{
				_this.antennaMap.delete(key);
			}
		});
	}
	delete this.antennaMap
	delete this.orientation
	delete this.position
	delete this.primity
	delete this.options
	delete this.model
    delete this.viewer
	return Cesium.destroyObject(this);
};
